/*
 * @lc app=leetcode.cn id=1848 lang=typescript
 *
 * [1848] 到目标元素的最小距离
 */

// @lc code=start
function getMinDistance(nums: number[], target: number, start: number): number {
    if (nums[start] === target) return 0;
    let after = start + 1;
    let before = start - 1;
    let count = Infinity;
    while (before >= 0) {
        if (nums[before] === target) {
            count = Math.min(count, start - before);
            break;
        }
        before--;
    }
    while (after < nums.length) {
        if (nums[after] === target) {
            count = Math.min(count, after - start);
            break;
        }
        after++;
    }
    return count;
}
// @lc code=end
